package org.poem.controller;

import com.google.gson.Gson;
import org.poem.entity.DemoObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/demoAnno")
public class DemoAnnoController {

    @RequestMapping("/index")
    public  @ResponseBody String index(){
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest();
        return  "url:"+ request.getRequestURI() + " can assess";
    }

    @RequestMapping("/demoPathVar/{id}")
    @ResponseBody
    public String demoPathVar(@PathVariable String id){
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest();
        return  "url:" + request.getRequestURI() + " can  assess, id is :"+ id;
    }

    @ResponseBody
    @RequestMapping("/getRequestParam")
    public String getRequestParam(String str){
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest();
        return  "url:" + request.getRequestURI() + " can  assess, str is :"+ str;
    }

    @ResponseBody
    @RequestMapping("/passEntity")
    public String passEntity(DemoObject demoObject){
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest();
        return  "url:" + request.getRequestURI() + " can  assess, DemoObject Json :"+ new Gson().toJson(demoObject);
    }

    @RequestMapping(value = {"/multiPath1","multiPath2"})
    @ResponseBody
    public String multiPath(){
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest();
        return  "url:" + request.getRequestURI() + " can  assess";
    }
}
